package main

import (
	"fmt"
	"os"
	"reflect"
	"time"
)

func main() {
	// mu := &sync.Mutex{}
	// c1:=sync.Cond{L: mu}
	// c2:=sync.Cond{L: mu}

	str := "hello"
	for k, v := range str {
		fmt.Println(k, string(v))
	}
	a := new(int) //a是一个指针
	fmt.Print(a)
	// c := make(chan any)
	s := make([]string, 0)
	s = append(s, "1")
	m := make(map[string]string)
	m["a"] = "a"
	m["c"] = "c"
	changeMap(m)
	fmt.Println(m)
	ch := make(chan int, 10)
	ch <- 1
	close(ch)
	// ch <- 2 // 通道关闭后不允许写，否则panic，但是可以取，值是类型的默认值

	ch_1 := <-ch
	ch_2 := <-ch
	fmt.Println(ch_1)
	fmt.Println(ch_2)

	// 输出到输出流中
	fmt.Fprintf(os.Stdout, "hello %d", 1)
	go func() {
		defer func() {
			if r := recover(); r != nil {
				fmt.Println(r)
			}
		}()
		panic("i am panic...")
	}()

	// 反射
	user := User{
		Name: "elon",
		Age:  31,
	}
	userType := reflect.TypeOf(user)
	userValue := reflect.ValueOf(user)
	userElem := userValue.Elem()
	fmt.Println(userElem.FieldByName("Name"))
	fmt.Print(userType)
	fmt.Print(userValue)
	time.Sleep(10 * time.Second)
	fmt.Print("end...")
	for {
		select {
		case cV, cOk := <-ch:
			fmt.Println("%v %v", cV, cOk)
		default:
			fmt.Println("default")
		}
	}
}

func changeMap(m map[string]string) {
	m["a"] = "aa"
	m["b"] = "b"
	delete(m, "c")
}

type User struct {
	Name string
	Age  int
}

func (u *User) name() string {
	return u.Name
}

func (u *User) age() int {
	return u.Age
}
